<?php
/* 	These code was generated using phpCIGen v 0.1.b (21/04/2009)
	#zaqi 		zaqi.smart@gmail.com,http://zenzaqi.blogspot.com, 
	
	+ Module  		: m_customer Controller
	+ Description	: For record controller process back-end
	+ Filename 		: C_m_customer.php
 	+ creator 		: 
 	+ Created on 06/Mar/2012 12:35:49
	
*/

//class of m_customer
class C_m_customer extends CI_Controller {

	//constructor
	function C_m_customer(){
		parent::__construct();
		session_start();
		$this->load->model('m_m_customer', '', TRUE);
	}
	
	//set index
	function index(){
		$this->load->view('main/v_m_customer');
	}
	
	//event handler action
	function get_action(){
		$task = $_POST['task'];
		switch($task){
			case "LIST":
				$this->m_customer_list();
				break;
			case "UPDATE":
				$this->m_customer_update();
				break;
			case "CREATE":
				$this->m_customer_create();
				break;
			case "DELETE":
				$this->m_customer_delete();
				break;
			case "SEARCH":
				$this->m_customer_search();
				break;
			case "PRINT":
				$this->m_customer_print();
				break;
			case "EXCEL":
				$this->m_customer_export_excel();
				break;
			default:
				echo "{failure:true}";
				break;
		}
	}
	
	//function fot list record
	function m_customer_list(){
		
		$query = isset($_POST['query']) ? @$_POST['query'] : "";
		$start = (integer) (isset($_POST['start']) ? @$_POST['start'] : @$_GET['start']);
		$end = (integer) (isset($_POST['limit']) ? @$_POST['limit'] : @$_GET['limit']);
		$result=$this->m_m_customer->m_customer_list($query,$start,$end);
		echo $result;
	}
	
	//function for create new record
	function m_customer_create(){
		//POST varible here
		//auto increment, don't accept anything from form values
		$customer_cp=trim(@$_POST["customer_cp"]);
		$customer_cp=htmlentities($customer_cp,ENT_QUOTES);
		$customer_hp=trim(@$_POST["customer_hp"]);
		$customer_hp=htmlentities($customer_hp,ENT_QUOTES);
		$customer_name=trim(@$_POST["customer_name"]);
		$customer_name=htmlentities($customer_name,ENT_QUOTES);
		$customer_address=trim(@$_POST["customer_address"]);
		$customer_address=htmlentities($customer_address,ENT_QUOTES);
		$customer_city=trim(@$_POST["customer_city"]);
		$customer_city=htmlentities($customer_city,ENT_QUOTES);
		$customer_post=trim(@$_POST["customer_post"]);
		$customer_post=htmlentities($customer_post,ENT_QUOTES);
		$customer_phone=trim(@$_POST["customer_phone"]);
		$customer_phone=htmlentities($customer_phone,ENT_QUOTES);
		$customer_fax=trim(@$_POST["customer_fax"]);
		$customer_fax=htmlentities($customer_fax,ENT_QUOTES);
		$customer_mail=trim(@$_POST["customer_mail"]);
		$customer_mail=htmlentities($customer_mail,ENT_QUOTES);
		$result=$this->m_m_customer->m_customer_create($customer_cp ,$customer_hp ,$customer_name ,$customer_address ,$customer_city ,$customer_post ,$customer_phone ,$customer_fax ,$customer_mail );
		echo $result;
	}
	
	
	//function for update record
	function m_customer_update(){
		//POST variable here
		$customer_id=@$_POST["customer_id"];
		$customer_id=is_numeric($customer_id)?$customer_id:0;
		$customer_cp=trim(@$_POST["customer_cp"]);
		$customer_cp=htmlentities($customer_cp,ENT_QUOTES);
		$customer_hp=trim(@$_POST["customer_hp"]);
		$customer_hp=htmlentities($customer_hp,ENT_QUOTES);
		$customer_name=trim(@$_POST["customer_name"]);
		$customer_name=htmlentities($customer_name,ENT_QUOTES);
		$customer_address=trim(@$_POST["customer_address"]);
		$customer_address=htmlentities($customer_address,ENT_QUOTES);
		$customer_city=trim(@$_POST["customer_city"]);
		$customer_city=htmlentities($customer_city,ENT_QUOTES);
		$customer_post=trim(@$_POST["customer_post"]);
		$customer_post=htmlentities($customer_post,ENT_QUOTES);
		$customer_phone=trim(@$_POST["customer_phone"]);
		$customer_phone=htmlentities($customer_phone,ENT_QUOTES);
		$customer_fax=trim(@$_POST["customer_fax"]);
		$customer_fax=htmlentities($customer_fax,ENT_QUOTES);
		$customer_mail=trim(@$_POST["customer_mail"]);
		$customer_mail=htmlentities($customer_mail,ENT_QUOTES);
		$result = $this->m_m_customer->m_customer_update($customer_id,$customer_cp,$customer_hp,$customer_name,$customer_address,$customer_city,$customer_post,$customer_phone,$customer_fax,$customer_mail);
		echo $result;
	}
	
	//function for delete selected record
	function m_customer_delete(){
		$ids = @$_POST['ids']; // Get our array back and translate it :
		$pkid = json_decode(stripslashes($ids));
		$result=$this->m_m_customer->m_customer_delete($pkid);
		echo $result;
	}

	//function for advanced search
	function m_customer_search(){
		//POST varibale here
		$customer_id=@$_POST["customer_id"];
		$customer_id=is_numeric($customer_id)?$customer_id:0;
		$customer_cp=trim(@$_POST["customer_cp"]);
		$customer_cp=htmlentities($customer_cp,ENT_QUOTES);
		$customer_hp=trim(@$_POST["customer_hp"]);
		$customer_hp=htmlentities($customer_hp,ENT_QUOTES);
		$customer_name=trim(@$_POST["customer_name"]);
		$customer_name=htmlentities($customer_name,ENT_QUOTES);
		$customer_address=trim(@$_POST["customer_address"]);
		$customer_address=htmlentities($customer_address,ENT_QUOTES);
		$customer_city=trim(@$_POST["customer_city"]);
		$customer_city=htmlentities($customer_city,ENT_QUOTES);
		$customer_post=trim(@$_POST["customer_post"]);
		$customer_post=htmlentities($customer_post,ENT_QUOTES);
		$customer_phone=trim(@$_POST["customer_phone"]);
		$customer_phone=htmlentities($customer_phone,ENT_QUOTES);
		$customer_fax=trim(@$_POST["customer_fax"]);
		$customer_fax=htmlentities($customer_fax,ENT_QUOTES);
		$customer_mail=trim(@$_POST["customer_mail"]);
		$customer_mail=htmlentities($customer_mail,ENT_QUOTES);
		
		$start = (integer) (isset($_POST['start']) ? $_POST['start'] : $_GET['start']);
		$end = (integer) (isset($_POST['limit']) ? $_POST['limit'] : $_GET['limit']);
		$result = $this->m_m_customer->m_customer_search($customer_id ,$customer_cp ,$customer_hp ,$customer_name ,$customer_address ,$customer_city ,$customer_post ,$customer_phone ,$customer_fax ,$customer_mail ,$start,$end);
		echo $result;
	}


	function m_customer_print(){
  		//POST varibale here
		$customer_id=@$_POST["customer_id"];
		$customer_id=is_numeric($customer_id)?$customer_id:0;
		$customer_cp=trim(@$_POST["customer_cp"]);
		$customer_cp=htmlentities($customer_cp,ENT_QUOTES);
		$customer_hp=trim(@$_POST["customer_hp"]);
		$customer_hp=htmlentities($customer_hp,ENT_QUOTES);
		$customer_name=trim(@$_POST["customer_name"]);
		$customer_name=htmlentities($customer_name,ENT_QUOTES);
		$customer_address=trim(@$_POST["customer_address"]);
		$customer_address=htmlentities($customer_address,ENT_QUOTES);
		$customer_city=trim(@$_POST["customer_city"]);
		$customer_city=htmlentities($customer_city,ENT_QUOTES);
		$customer_post=trim(@$_POST["customer_post"]);
		$customer_post=htmlentities($customer_post,ENT_QUOTES);
		$customer_phone=trim(@$_POST["customer_phone"]);
		$customer_phone=htmlentities($customer_phone,ENT_QUOTES);
		$customer_fax=trim(@$_POST["customer_fax"]);
		$customer_fax=htmlentities($customer_fax,ENT_QUOTES);
		$customer_mail=trim(@$_POST["customer_mail"]);
		$customer_mail=htmlentities($customer_mail,ENT_QUOTES);
		$option=$_POST['currentlisting'];
		$filter=$_POST["query"];
		
		$data["data_print"] = $this->m_m_customer->m_customer_print($customer_id ,$customer_cp ,$customer_hp ,$customer_name ,$customer_address ,$customer_city ,$customer_post ,$customer_phone ,$customer_fax ,$customer_mail ,$option,$filter);
		$print_view=$this->load->view("main/p_m_customer.php",$data,TRUE);
		if(!file_exists("print")){
			mkdir("print");
		}
		$print_file=fopen("print/m_customer_printlist.html","w+");
		fwrite($print_file, $print_view);
		echo '1';        
	}
	/* End Of Function */

	/* Function to Export Excel document */
	function m_customer_export_excel(){
		//POST varibale here
		$customer_id=@$_POST["customer_id"];
		$customer_id=is_numeric($customer_id)?$customer_id:0;
		$customer_cp=trim(@$_POST["customer_cp"]);
		$customer_cp=htmlentities($customer_cp,ENT_QUOTES);
		$customer_hp=trim(@$_POST["customer_hp"]);
		$customer_hp=htmlentities($customer_hp,ENT_QUOTES);
		$customer_name=trim(@$_POST["customer_name"]);
		$customer_name=htmlentities($customer_name,ENT_QUOTES);
		$customer_address=trim(@$_POST["customer_address"]);
		$customer_address=htmlentities($customer_address,ENT_QUOTES);
		$customer_city=trim(@$_POST["customer_city"]);
		$customer_city=htmlentities($customer_city,ENT_QUOTES);
		$customer_post=trim(@$_POST["customer_post"]);
		$customer_post=htmlentities($customer_post,ENT_QUOTES);
		$customer_phone=trim(@$_POST["customer_phone"]);
		$customer_phone=htmlentities($customer_phone,ENT_QUOTES);
		$customer_fax=trim(@$_POST["customer_fax"]);
		$customer_fax=htmlentities($customer_fax,ENT_QUOTES);
		$customer_mail=trim(@$_POST["customer_mail"]);
		$customer_mail=htmlentities($customer_mail,ENT_QUOTES);
		$option=$_POST['currentlisting'];
		$filter=$_POST["query"];
		
		$query = $this->m_m_customer->m_customer_export_excel($customer_id ,$customer_cp ,$customer_hp ,$customer_name ,$customer_address ,$customer_city ,$customer_post ,$customer_phone ,$customer_fax ,$customer_mail ,$option,$filter);
		
		to_excel($query,"m_customer"); 
		echo '1';
			
	}
	
	// Encodes a SQL array into a JSON formated string
	function JEncode($arr){
		if (version_compare(PHP_VERSION,"5.2","<"))
		{    
			require_once("./JSON.php"); //if php<5.2 need JSON class
			$json = new Services_JSON();//instantiate new json object
			$data=$json->encode($arr);  //encode the data in json format
		} else {
			$data = json_encode($arr);  //encode the data in json format
		}
		return $data;
	}
	
	// Decode a SQL array into a JSON formated string
	function JDecode($arr){
		if (version_compare(PHP_VERSION,"5.2","<"))
		{    
			require_once("./JSON.php"); //if php<5.2 need JSON class
			$json = new Services_JSON();//instantiate new json object
			$data=$json->decode($arr);  //decode the data in json format
		} else {
			$data = json_decode($arr);  //decode the data in json format
		}
		return $data;
	}
	
	
}
?>